package com.motion.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.motion.generator.entity.UserCourse;
import com.motion.generator.entity.UserDynamic;
import com.motion.generator.mapper.UserCourseMapper;
import com.motion.generator.mapper.UserDynamicMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.time.LocalTime;

@Service
public class UserDynamicService {
    @Autowired(required = false)
    UserCourseMapper userCourseMapper;
    @Autowired(required = false)
    UserDynamicMapper userDynamicMapper;
    //课程完成页面
    public LocalDateTime courseCompletedRevisionTime(Long userId, int time){
        try {
            QueryWrapper<UserDynamic> queryWrapper1 = new QueryWrapper<>();
            queryWrapper1.eq("userId", userId);
            UserDynamic userDynamic = userDynamicMapper.selectOne(queryWrapper1);
            if(userDynamic!=null) {
                LocalDateTime localDateTime = userDynamic.getLastDay();
                userDynamic.setTrainDuration(userDynamic.getTrainDuration() + time);
                userDynamic.setLastDay(LocalDateTime.now());
                userDynamicMapper.updateById(userDynamic);
                return localDateTime;
            }else{
                UserDynamic userDynamicTem=new UserDynamic();
                userDynamicTem.setTrainDuration(time);
                userDynamicTem.setUserId(userId);
                userDynamicTem.setFansNumber(0);
                userDynamicTem.setLastDay(LocalDateTime.now());
                userDynamicMapper.insert(userDynamicTem);
                return userDynamicTem.getLastDay();
            }
        }catch (Exception e){
            return null;
        }
    }
    //课程页面查询
    public UserDynamic getUserDynamicByUserId(Long userId){
        QueryWrapper<UserDynamic> queryWrapper1 = new QueryWrapper<>();
        queryWrapper1.eq("userId", userId);
        UserDynamic userDynamic = userDynamicMapper.selectOne(queryWrapper1);
        if(userDynamic!=null){
            return userDynamic;
        }else{
            UserDynamic userDynamicTem=new UserDynamic();
            userDynamicTem.setTrainDuration(0);
            userDynamicTem.setUserId(userId);
            userDynamicTem.setFansNumber(0);
            userDynamicTem.setLastDay(LocalDateTime.of(2001,4,6,12,45,5));
            userDynamicMapper.insert(userDynamicTem);
            return userDynamicTem;
        }
    }

}
